import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stort/html/banner_dao.dart';
import 'package:flutter_stort/html/goods_dao.dart';
import 'package:flutter_stort/model/goods_entity.dart';
import 'package:flutter_stort/utils/toast_utils.dart';
import 'package:flutter_stort/widget/banner_view.dart';
import 'package:flutter_stort/widget/load_image.dart';
import 'package:flutter_stort/widget/load_state_view.dart';

// ignore: must_be_immutable
class GoodsDetailsPage extends StatefulWidget {
  GoodsEntity goods;
  GoodsDetailsPage({this.goods});

  @override
  _GoodsDetailsPageState createState() => _GoodsDetailsPageState();
}

class _GoodsDetailsPageState extends State<GoodsDetailsPage> {
  List<Widget> _listContent = [];
  LoadState _loadState = LoadState.LOADING;

  ///获取商品轮播图
  void _getBanner() {
    BannerDao.getDetailsBanner(1).then((value) {
      setState(() {
        if (value != null && value.length > 0) {
          _listContent.insert(
            0,
            BannerView(
              width: double.infinity,
              height: 260,
              listBanner: value,
            ),
          );
          _loadState = LoadState.SUCCESS;
        }
      });
    });
  }

  ///商品详情界面
  void _goodsDetails() {
    setState(() {
      _listContent.add(Container(
        color: Colors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 10),
            Container(
              padding: EdgeInsets.all(10),
              child: Text(
                widget.goods.title,
                textAlign: TextAlign.left,
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 18,
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
              child: Text(
                widget.goods.content,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: Colors.grey,
                  fontSize: 14,
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
              child: Text(
                "￥${widget.goods.price}",
                style: TextStyle(
                  color: Colors.red,
                  fontSize: 14,
                ),
              ),
            ),
            Divider(
              color: Colors.grey,
              height: 1,
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                    flex: 1,
                    child: Text(
                      "运费：${widget.goods.freight}",
                      style: TextStyle(
                        color: Colors.grey,
                        fontSize: 14,
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Text(
                      "剩余：${widget.goods.stock}",
                      style: TextStyle(
                        color: Colors.grey,
                        fontSize: 14,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Divider(
              color: Colors.grey,
              height: 1,
            ),
            InkWell(
              onTap: () {},
              child: Container(
                height: 50,
                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                child: Row(
                  children: [
                    Expanded(
                      child: Text(
                        "查看商品评价",
                        style: TextStyle(
                          color: Colors.black54,
                          fontSize: 16,
                        ),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      color: Colors.grey,
                      size: 18,
                    ),
                  ],
                ),
              ),
            ),
            Divider(color: Colors.grey, height: 1),
          ],
        ),
      ));
    });
  }

  ///获取详情图片
  void _goodsImageDetails() {
    GoodsDao.getGoodsImg().then((value) {
      setState(() {
        for (var i = 0; i < value.length; i++) {
          _listContent.add(LoadImageView(
            width: double.infinity,
            imageUrl: value[i],
            height: 250,
          ));
        }
      });
    });
  }

  @override
  void initState() {
    _getBanner();
    _goodsDetails();
    _goodsImageDetails();
    super.initState();
  }

  ///显示马上购买
  void _showBuyNow() {}

  ///加入购物车
  void _addShoppingCart() {
    ToastUtil.showToast("购物车添加成功，该商品已经添加进购物车。");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.goods.title,
          style: TextStyle(
            fontSize: 20,
          ),
        ),
      ),
      body: LoadStateView(
        loadState: _loadState,
        erroRetry: () {},
        successWidget: Container(
          child: Column(
            children: [
              Expanded(
                flex: 1,
                child: ListView(
                  children: _listContent,
                ),
              ),
              Container(
                height: 50,
                child: Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: InkWell(
                        splashColor: Colors.white.withOpacity(0.5),
                        onTap: _addShoppingCart,
                        child: Container(
                          color: Colors.green,
                          alignment: Alignment.center,
                          child: Text(
                            "加入购物车",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                            ),
                          ),
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: InkWell(
                        onTap: _showBuyNow,
                        splashColor: Colors.white.withOpacity(0.5),
                        child: Container(
                          alignment: Alignment.center,
                          color: Colors.red,
                          child: Text(
                            "马上购买",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
